//
//  FourViewController.swift
//  学唐诗
//
//  Created by 申克 on 16/5/12.
//  Copyright © 2016年 shang_hongzhi. All rights reserved.
//

import UIKit

class FourViewController: UIViewController {
    
   
    let imageArr = NSMutableArray()
    
    var DBView:DBSphereView! = nil
    
    var nameButton:UIButton! = nil
    
    var names : NSArray!
    var titles : NSArray!
    var titlesDict : NSDictionary!
    var poertys: NSDictionary!
    
    var poetry : String!
    
    @IBOutlet weak var myLabel: UILabel!
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.redColor()
        // Do any additional setup after loading the view.
        let path = NSBundle.mainBundle().pathForResource("poetrys", ofType: "plist")
        titlesDict = NSDictionary.init(contentsOfFile: path!)
        names = titlesDict.allKeys
        
        let buttonArr = NSMutableArray()
        DBView = DBSphereView.init(frame: CGRectMake(20, 20, UIScreen.mainScreen().bounds.width/2, UIScreen.mainScreen().bounds.height))
        for index in 0 ..< names.count {
            nameButton = UIButton.init(type: UIButtonType.Custom)
            nameButton.frame = CGRectMake(0, 0, 50, 50)
            nameButton.tag = index
            nameButton.setTitle(names[index]as? String, forState: UIControlState.Normal)
            nameButton.titleLabel?.font = UIFont.systemFontOfSize(16)
            nameButton.backgroundColor = self.randomColor()
            nameButton.setTitleColor(self.randomColor(), forState: UIControlState.Normal)
            nameButton.addTarget(self, action: #selector(FourViewController.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            DBView.addSubview(nameButton)
            buttonArr.addObject(nameButton)
        }
         DBView.setCloudTags(buttonArr as [AnyObject])
         self.view .addSubview(DBView)
        
//        let girl = UIImageView.init(frame: CGRectMake(UIScreen.mainScreen().bounds.width/2+100, 60, 140, 150))
//
//        self.view.addSubview(girl)
//        for index in 1..<4 {
//            let imageName = NSString.init(format: "image_%d", index)
//            let path = NSBundle.mainBundle().pathForResource(imageName as String, ofType: "jpg")!
//            let images = UIImage.init(contentsOfFile: path)
//            imageArr.addObject(images!)
//        }
////        let timer = 0.1
//        girl.animationImages = NSArray.init(array: imageArr) as? [UIImage]
//        girl.animationDuration = 0.5
//        girl.startAnimating()
        
        
        let button = UIButton.init(type: UIButtonType.Custom)
        button.frame = CGRectMake(10, 0, 60, 40)
        button.setTitle("返回", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.clearColor()
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.view.addSubview(button)
        button.addTarget(self, action: #selector(SecondViewController.returnButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        myLabel.text = "请选择一位你喜欢的诗人吧"
        myLabel.textColor = UIColor.whiteColor()
        myLabel.font = UIFont.systemFontOfSize(20)
    }
    
    override func viewWillAppear(animated: Bool) {
        DBView.timerStart()
    }
    func returnButtonClick(){
        self.navigationController?.popViewControllerAnimated(true)
        
    }
    
    func buttonClick(button:UIButton){
        DBView.timerStop()
        let second = SecondViewController()
        second.poetrys = titlesDict.objectForKey((button.titleLabel?.text)!) as! NSDictionary
        second.name = button.titleLabel?.text
        self.navigationController?.pushViewController(second, animated: true)
 
    }
    
    func randomColor() -> UIColor {
        let red = CGFloat(arc4random() % 255)
        let green = CGFloat(arc4random() % 255)
        let blue = CGFloat(arc4random() % 255)
        return UIColor.init(red: red/255, green: green/255, blue: blue/255, alpha: 1)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
